package test.iceiceice.com.apptest;

import android.app.Application;
import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by 尹程 on 2017/4/5.
 */

public class MiniApp extends Application {
    public static Context context;
    public static OkHttpClient okHttpClient;
    public static OkHttpUtils okHttpUtils;
    private static MiniApp instance;


    @Override
    public void onCreate() {
        super.onCreate();
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .addInterceptor(httpLoggingInterceptor)
                .build();
        okHttpUtils = OkHttpUtils.initClient(okHttpClient);

    }


    static Toast mToast;

    public static void showToast(String message) {
        if (null == mToast) {
            mToast = Toast.makeText(context, message,
                    Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mToast.setText(message);
        }
        mToast.show();
    }

    public synchronized static MiniApp getInstance() {
        if (null == instance) {
            instance = new MiniApp();
        }
        return instance;
    }


    public static void setContext(Context context) {
        MiniApp.context = context;
    }


}
